import { AnimalDatas } from "./GameDefine";
import { GameData } from "./GameData";
import { GameState } from "./GameState";
import { EventKey } from "../common/Key";
import { TaskMgr } from "./TaskMgr";

cc.Class({
    extends: require('BaseUI'),

    properties: {
        sprAnimal: cc.Sprite,
        labIndex: cc.Label,
        labName: cc.Label,
        labCoinPrice: cc.Label,
        labDiamondPrice: cc.Label,
        nodBtnBuyByCoin: cc.Node,
        nodBtnBuyByDiamond: cc.Node,
        nodCantBuy: cc.Node,
        nodCoin: cc.Node,
        matr: cc.Material,
    },
    onLoad() {
        this._super();
    },

    onEnable() {
        if (!this._inited) {
            return;
        }
        this.updateUI();
        this.updateBtn();
        this._updateCoinBtn();
        this._updateDiamondBtn();

        this._registEvent();
    },
    onDisable(){
        this._unRegistEvent();
    },

    init(index) {
        this._index = index;
        this._inited = true;
        let data = AnimalDatas[index];
        this.labIndex.string = 'LV.' + (index + 1);
       
        let url = 'textures/animals/' + data.icon;
        G.common.setSpriteFrame(this.sprAnimal, url);

        this.updateUI();
        this.updateBtn();
        this._updateCoinBtn();
        this._updateDiamondBtn();
    },

    updateUI() {
        let data = AnimalDatas[this._index];
        let currIndex = GameData.getCurrAnimalIndex();
        if (this._index > currIndex) {
            this.sprAnimal.setMaterial(0, this.matr);
            this.labName.string = '???';
        } else {
            this.sprAnimal.setMaterial(0, cc.Material.getInstantiatedBuiltinMaterial('2d-sprite'));
            this.labName.string = data.name;
        }
    },

    updateBtn() {
        let canBuyByCoin = GameState.getCanBuyByCoin(this._index);
        if (canBuyByCoin) {
            this.nodBtnBuyByCoin.active = true;
            this.nodBtnBuyByDiamond.active = false;
            this.nodCantBuy.active = false;
            return;
        }
        let canBuyByDiamond = GameState.getCanBuyByDiamond(this._index);
        if (canBuyByDiamond) {
            this.nodBtnBuyByCoin.active = false;
            this.nodBtnBuyByDiamond.active = true;
            this.nodCantBuy.active = false;
            return;
        }
        this.nodBtnBuyByCoin.active = false;
        this.nodBtnBuyByDiamond.active = false;
        this.nodCantBuy.active = true;
    },

    _updateCoinBtn() {
        if (!this.nodBtnBuyByCoin.active) {
            return;
        }
        let currCoin = GameData.getCurrCoin();
        let price = GameState.getAnimalCoinPrice(this._index);
        this.labCoinPrice.string = G.bignumber.toUnitType(price);
        if (G.bignumber.compose(currCoin, price) == -1) {
            this.nodBtnBuyByCoin.getComponent(cc.Button).interactable = false;
        } else {
            this.nodBtnBuyByCoin.getComponent(cc.Button).interactable = true;
        }
    },
    _updateDiamondBtn() {
        if (!this.nodBtnBuyByDiamond.active) {
            return;
        }
        let diamond = GameData.getDiamond();
        let price = GameState.getAnimalDiamondPrice(this._index);
        this.labDiamondPrice.string = price;
        if (G.bignumber.compose(diamond, price) == -1) {
            this.nodBtnBuyByDiamond.getComponent(cc.Button).interactable = false;
        } else {
            this.nodBtnBuyByDiamond.getComponent(cc.Button).interactable = true;
        }
    },

    buttonListener: function (button) {
        G.game.gameAudioMgr.playBtn();
        let name = button.node.name;
        if (name == 'btnBuyByCoin') {
            let maxCount = GameData.getMaxAnimalCount();
            let aliveAnimalData = GameData.getAliveAnimalData();
            if (aliveAnimalData.length >= maxCount) {
                G.game.toast('场上动物数量已达上限', 1);
                return;
            }

            let currCoin = GameData.getCurrCoin();
            let price = GameState.getAnimalCoinPrice(this._index);

            if (G.bignumber.compose(currCoin, price) == -1) {
                // G.game.toast('金币不足', 1);
                G.ui.openUI('PanelAddCoin');
                return;
            }
            GameData.subCoin(price);
            GameData.addAnimalBuyCount(this._index);

            G.onFire.fire(EventKey.EVENT_GEN_ANIMAL, { index: this._index });
            G.onFire.fire(EventKey.EVENT_UPDATE_NEXT_ANIMAL_PRICE);
            G.game.toast('购买成功', 1);
            TaskMgr.addDaliyTask(1,1);
        } else if (name == 'btnBuyByDiamond') {
            let maxCount = GameData.getMaxAnimalCount();
            let aliveAnimalData = GameData.getAliveAnimalData();
            if (aliveAnimalData.length >= maxCount) {
                G.game.toast('场上动物数量已达上限', 1);
                return;
            }

            let diamond = GameData.getDiamond();
            let price = GameState.getAnimalDiamondPrice(this._index);

            if (G.bignumber.compose(diamond, price) == -1) {
                G.game.toast('钻石不足', 1);
                return;
            }
            GameData.addDiamond(-price);
            GameData.addAnimalBuyCount(this._index);

            G.onFire.fire(EventKey.EVENT_GEN_ANIMAL, { index: this._index });
            G.onFire.fire(EventKey.EVENT_UPDATE_NEXT_ANIMAL_PRICE);
            G.game.toast('购买成功', 1);
            TaskMgr.addDaliyTask(1,1);
        }
    },

    _onFire(eventName, param) {
        if (eventName == EventKey.EVENT_UPDATE_COIN) {
            this._updateCoinBtn();
        } else if (eventName == EventKey.EVENT_UPDATE_DIAMOND) {
            this._updateDiamondBtn();
        }
    },

    _registEvent() {
        G.onFire.on(EventKey.EVENT_UPDATE_COIN, this._onFire, this);
        G.onFire.on(EventKey.EVENT_UPDATE_DIAMOND, this._onFire, this);
    },
    _unRegistEvent() {
        G.onFire.off(EventKey.EVENT_UPDATE_COIN, this._onFire, this);
        G.onFire.off(EventKey.EVENT_UPDATE_DIAMOND, this._onFire, this);
    },
});
